<template>
	<view class="address-container">
		<!-- 没有收货地址信息时 -->
		<view class="add-address-box" v-if="JSON.stringify(address) === '{}'">
			<button type="primary" size="mini" @click="chooseAddress">添加收货地址+</button>
		</view>
		<!-- 有收货地址信息时 -->
		<view class="address-info" v-else @click="chooseAddress">
			<!-- 第一行电话姓名和电话 -->
			<view class="row1-box">
				<view class="row1-left">
					<view>收货人姓名：</view>
					<view class="username">{{address.userName}}</view>
				</view>
				<view class="row1-right">
					<view>电话：</view>
					<view class="phoneNum">{{address.telNumber}}
					<uni-icons type="arrowright" size="16"></uni-icons>
					</view>
				</view>
			</view>
			<!-- 第二行收货地址 -->
			<view class="row2-box">
				<view>收货地址：</view>
				<view class="address-detail">{{addstr}}</view>
			</view>
		</view>
		<!-- 下边框分割线 -->
		<image src="../../static/cart_border@2x.png" class="cart-border"></image>
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from 'vuex'
	export default {
		name:"my-address",
		data() {
			return {
				// address:{}
			};
		},
		methods:{
			...mapMutations('my_user',['updateAddress']),
			async chooseAddress(){
				// 1. 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
				//    返回值是一个数组：第1项为错误对象；第2项为成功之后的收货地址对象
				const [err,succ] = await uni.chooseAddress().catch(err => err)
				// 2. 用户成功的选择了收货地址
				if(succ && succ.errMsg === 'chooseAddress:ok'){
				// console.log(succ)
					// this.address = succ
					// 更新 vuex 中的收货地址
					this.updateAddress(succ)
				}
				// 3. 用户没有授权
				if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) {
					// 通过调用这个方法，让用户重新授权
					this.reAuth()
				}
			},
			// 让用户重新授权
		  async reAuth() {
			const [err2, confirmResult] = await uni.showModal({
			  content: '检测到您没打开地址权限，是否去设置打开？',
			  confirmText: '确认',
			  cancelText: '取消'
			})
	
			if (err2) return
	
			// console.log(confirmResult)
	
			if (confirmResult.cancel) return uni.$showMsg('您取消了地址授权！')
			if (confirmResult.confirm) return uni.openSetting({
			  success: (settingResult) => {
				if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了授权！')
				if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
			  }
			})
		  }
		},	
		computed:{
			...mapState('my_user',['address']),
			...mapGetters('my_user',['addstr'])
		}
	}
</script>

<style lang="scss">
.address-container{
	background-color: #f5f5f5;
	.add-address-box{
		display: flex;
		height: 90px;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.cart-border{
		display: block;
		height: 5px;
		width: 100%;
	}
	.address-info{
		font-size: 13px;
		padding: 0 8px;
		margin-bottom: 10px;
		.row1-box{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.row1-left{
				display: flex;
				align-items: center;
			}
			.row1-right{
				display: flex;
				align-items: center;
			}
		}
		.row2-box{
			display: flex;
			align-items: center;
			margin-top: 10px;
		}
	}
}
</style>
